package com.meida.paotui.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.model.ListModel;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.security.OpenHelper;
import com.meida.common.utils.FlymeUtils;
import com.meida.common.utils.JsonUtils;
import com.meida.common.utils.OrderNoUtil;
import com.meida.module.pay.client.entity.PayInfo;
import com.meida.module.pay.provider.service.PayInfoService;
import com.meida.module.user.provider.service.AppUserService;
import com.meida.paotui.bus.client.entity.BusCommonaddress;
import com.meida.paotui.bus.client.entity.BusCustomer;
import com.meida.paotui.bus.client.entity.BusCustomerdetail;
import com.meida.paotui.bus.client.entity.BusTrade;
import com.meida.paotui.bus.provider.mapper.BusTradeMapper;
import com.meida.paotui.bus.provider.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;

@RestController
@Api(tags = "常用地址")
public class CommonAddressController {
    @Autowired
    private BusCommonaddressService buscommonaddressService;
    @Autowired
    private BusScheduleService busScheduleService;

    @Autowired
    private PayInfoService payInfoService;

    @Autowired
    private AppUserService userService;

    @Autowired
    private BusTradeService tradeService;
    @Autowired
    private BusCustomerService customerService;
    @Autowired
    private OrderNoUtil orderNoUtil;
    @Resource
    private BusTradeMapper busTradeMapper;
    @Autowired
    private BusTradeService busTradeService;
    @Autowired
    private BusCustomerdetailService busCustomerdetailService;


    /**
     * 获取常用地址列表
     *
     * @param
     * @return
     */
    @PostMapping(value = "/address/getCommonAddress")
    public ResultBody list(String address) {
        ListModel model = new ListModel();
        EntityMap map = new EntityMap();
        map.put("address", address);
        model.setParams(JsonUtils.beanToJson(map));
        return buscommonaddressService.listEntityMap(model);
    }


    @ApiOperation(value = "用户常用地址-添加", notes = "添加用户常用地址")
    @PostMapping(value = "/address/saveOrUpdate")
    public ResultBody save(@ModelAttribute(value = "updateModel") BusCommonaddress commonaddress, ResultBody resultBody) {
        QueryWrapper<BusCommonaddress> we=new QueryWrapper();
        we.eq("lng",commonaddress.getLng());
        we.eq("lat",commonaddress.getLat());
        we.eq("userId",OpenHelper.getUserId());
        BusCommonaddress address=buscommonaddressService.getOne(we);
        if (FlymeUtils.isNotEmpty(address)) {
            address.setUserId(OpenHelper.getUserId());
            address.setMobile(commonaddress.getMobile());
            address.setAddress(commonaddress.getAddress());
            address.setName(commonaddress.getName());
            address.setDetailAdress(commonaddress.getDetailAdress());
            boolean a =buscommonaddressService.saveOrUpdate(address);
            return  ResultBody.ok(address);
        } else{
            commonaddress.setUserId(OpenHelper.getUserId());
            buscommonaddressService.saveOrUpdate(commonaddress);
            return  ResultBody.ok(commonaddress);
        }
    }


    @ApiOperation(value = "用户常用地址-删除", notes = "删除常用地址")
    @PostMapping(value = "/address/delete")
    public ResultBody delete(String commonAddressId, ResultBody resultBody) {
        Boolean a = buscommonaddressService.removeById(commonAddressId);
        if (a) {
            resultBody.setMsg("删除成功");
        } else {
            resultBody.code(101);
        }
        return resultBody;
    }

    @PostMapping(value = "/test/goodsaccount")
    public ResultBody goodsaccount(String outTradeNo) {
        PayInfo info = payInfoService.findByOutTradeNo(outTradeNo);
        if (FlymeUtils.isNotEmpty(info)) {
            BusCustomer customer = customerService.getById(info.getBusId());
            EntityMap map = new EntityMap();
            map.put("userId", info.getUserId());
            EntityMap use_map = busTradeMapper.findblance(map);
            if (customer.getRechargeType().equals("1")) {
                BigDecimal balance = use_map.getBigDecimal("balance");
                BigDecimal newbalance = balance.add(info.getOrderAmount());
                BusTrade trade = new BusTrade();
                trade.setBalance(newbalance);
                trade.setBusinessId(outTradeNo);
                trade.setTradeExplain("账户充值");
                trade.setTradeSum(info.getOrderAmount());
                trade.setTradeType(1);
                trade.setType(0);
                trade.setUserId(OpenHelper.getUserId());
                trade.setStatus(1);
                boolean a = busTradeService.save(trade);
            }
            if (customer.getRechargeType().equals("2")) {
                BigDecimal balance = use_map.getBigDecimal("balance");
                BusTrade trade = new BusTrade();
                trade.setBusinessId(outTradeNo);
                trade.setTradeExplain("优惠券充值");
                trade.setTradeSum(info.getOrderAmount());
                trade.setTradeType(1);
                trade.setType(0);
                trade.setUserId(OpenHelper.getUserId());
                trade.setBalance(balance);
                trade.setStatus(1);
                boolean a = busTradeService.save(trade);
                BusCustomer customer1 = customerService.getById(info.getBusId());
                QueryWrapper<BusCustomerdetail> qw_d = new QueryWrapper<>();
                qw_d.eq("userId", info.getUserId());
                qw_d.eq("customerId", info.getBusId());
                BusCustomerdetail customerdetail = busCustomerdetailService.getOne(qw_d);
                if (FlymeUtils.isNotEmpty(customerdetail)) {
                    customerdetail.setCoupCount(customerdetail.getCoupCount() + customer1.getTicketCount());
                    busCustomerdetailService.saveOrUpdate(customerdetail);
                } else {
                    BusCustomerdetail customerdetail_new = new BusCustomerdetail();
                    customerdetail_new.setCoupCount(customer1.getTicketCount());
                    customerdetail_new.setCustomerId(customer1.getCustomerId());
                    customerdetail_new.setUserId(info.getUserId());
                    busCustomerdetailService.save(customerdetail_new);
                }
            }
        }
             return ResultBody.ok();
    }
}


